<?php
namespace app\admin\controller;

use app\admin\model\Admin as AD;
use app\Request;
use think\validate\ValidateRule;
use app\admin\model\Node as NoModel;

class Node extends Base
{
    protected $middleware = [
        '\app\admin\middleware\LoginCheck',
        '\app\admin\middleware\UrlData',
        '\app\admin\middleware\UrlCheck'

    ];

    public function index(){
        return view('index');
    }

    public function data()
    {
        //$list = $this->record();
        $list = NoModel::select();
        $count = NoModel::count();

        return json([ "code"=>0,"msg"=>"查询成功","count"=>$count,"data"=>$list]);
    }

    public function add(Request $request)
    {
        if($this->request->isPost()){
            $data = $this->request->post();

            $mode = $this->mode($request);
            $url = "/".$mode."/".$data['c_name']."/".$data["a_name"];
            unset($data['c_name'],$data['a_name']);
            $data['node_url'] = $url;

            $node = explode('/',$data['partent_level']);
            unset($data['partent_level']);
            $data['node_partent'] = $node['0'];
            $data['node_grade'] = $node['1'];

            
            $user = new NoModel();
            $res = $user->save($data);
            if($res){
                $this->success("权限添加成功","index");
            }
            $this->error("权限添加失败");
        }

        $list = $this->record();
        //var_dump($list->toArray());die();
        return view("",["list"=>$list]);
    }

    public function update($id ,Request $request){
        if($this->request->isPost()){
            $data = $this->request->post();
            //处理节点等级 以及节点URL

            $mode = $this->mode($request);

            $url = "/".$mode."/".$data['c_name']."/".$data["a_name"];
            unset($data['c_name'],$data['a_name']);
            $data['node_url'] = $url;

            $node = explode('/',$data['partent_level']);
            unset($data['partent_level']);
            $data['node_partent'] = $node['0'];
            $data['node_grade'] = $node['1'];


            //数据库修改
            $user = NoModel::find($data['id']);
            unset($data['id']);
            $res = $user->save($data);

            if($res){
                $this->success("权限修改成功","index");
            }
            $this->error("权限修改失败");

            //var_dump($data);die();
        }

        $list = $this->record();
        $info = NoModel::where('id',$id)->find();

        if(!empty($info['node_url'])){
        $urls = explode('/',$info['node_url']);
        $info['c'] = $urls[2];
        $info['a'] = $urls[3];
        }else{
            $info['c'] = "";
            $info['a'] = "";
        }
        unset($info['node_url']);

        return view('add',['list'=>$list,'info'=>$info]);
    }

    public function del($id){
        $pid = NoModel::where('node_partent',$id)->select()->toArray();
        if(empty($pid)){  //判断他是否有子节点,如果为空就删除，不为空就不允许删除
            $res = NoModel::where('id',$id)->delete();
        }else{
            return json(['code'=>400,'msg'=>"权限子节点不为空，请删除子节点后再进行删除"]);
        }


        if($res){
            return json(['code'=>0,'msg'=>"权限删除成功"]);
        }
        return json(['code'=>400,'msg'=>"权限删除成功"]);
    }

    public function record($pid = 0){
        $list = NoModel::where("node_partent",$pid)->select();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $children = $this->record($v['id']);
                if(!empty($children)){
                    $list[$k]["children"] = $children;
                }
            }
        }
        return $list;
    }

    private function mode(Request $request){ //自动tp应用名
        $url = $request->baseUrl();
        $mode = explode('/',$url);

        return $mode[1];
    }
}